/**
 ******************************************************************************
 * @file    atu.c
 * @author  hyseim software Team
 * @date    01-Sep-2023
 * @brief   This file provides all the spi functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "common.h"
#include "atu.h"



/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
 * @brief  Initializes the ATU0 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU0 peripheral.
 * @return None
 */
void ATU0_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU0.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU0.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU0.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU0.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU0.TIORH.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU0.TIORH.BIT.IOB = ATU_InitStruct->ATU_pinBMode;
    ATU0.TIORL.BIT.IOC = ATU_InitStruct->ATU_pinCMode;
    ATU0.TIORL.BIT.IOD = ATU_InitStruct->ATU_pinDMode;
}

/**
 * @brief  Initializes the ATU1 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU1 peripheral.
 * @return None
 */
void ATU1_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU1.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU1.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU1.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU1.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU1.TIOR.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU1.TIOR.BIT.IOB = ATU_InitStruct->ATU_pinBMode;
}

/**
 * @brief  Initializes the ATU2 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU2 peripheral.
 * @return None
 */
void ATU2_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU2.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU2.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU2.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU2.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU2.TIOR.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU2.TIOR.BIT.IOB = ATU_InitStruct->ATU_pinBMode;  
}

/**
 * @brief  Initializes the ATU3 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU3 peripheral.
 * @return None
 */
void ATU3_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU3.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU3.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU3.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU3.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU3.TIORH.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU3.TIORH.BIT.IOB = ATU_InitStruct->ATU_pinBMode;
    ATU3.TIORL.BIT.IOC = ATU_InitStruct->ATU_pinCMode;
    ATU3.TIORL.BIT.IOD = ATU_InitStruct->ATU_pinDMode; 
}

/**
 * @brief  Initializes the ATU4 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU4 peripheral.
 * @return None
 */
void ATU4_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU4.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU4.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU4.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU4.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU4.TIORH.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU4.TIORH.BIT.IOB = ATU_InitStruct->ATU_pinBMode;
    ATU4.TIORL.BIT.IOC = ATU_InitStruct->ATU_pinCMode;
    ATU4.TIORL.BIT.IOD = ATU_InitStruct->ATU_pinDMode; 
}

/**
 * @brief  Initializes the ATU6 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU6 peripheral.
 * @return None
 */
void ATU6_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU6.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU6.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU6.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU6.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU6.TIORH.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU6.TIORH.BIT.IOB = ATU_InitStruct->ATU_pinBMode;
    ATU6.TIORL.BIT.IOC = ATU_InitStruct->ATU_pinCMode;
    ATU6.TIORL.BIT.IOD = ATU_InitStruct->ATU_pinDMode;
}

/**
 * @brief  Initializes the ATU7 peripheral according to the specified 
 *         parameters in the ATU_InitStruct.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU7 peripheral.
 * @return None
 */
void ATU7_Init(ATU_InitTypeDef* ATU_InitStruct)
{
    ATU7.TMDR1.BIT.MD = ATU_InitStruct->ATU_mode;
    ATU7.TCR.BIT.TPSC = ATU_InitStruct->ATU_divClk;
    ATU7.TCR.BIT.CKEG = ATU_InitStruct->ATU_selectClkEdge;
    ATU7.TCR.BIT.CCLR = ATU_InitStruct->ATU_cntClearing;
    ATU7.TIORH.BIT.IOA = ATU_InitStruct->ATU_pinAMode;
    ATU7.TIORH.BIT.IOB = ATU_InitStruct->ATU_pinBMode;
    ATU7.TIORL.BIT.IOC = ATU_InitStruct->ATU_pinCMode;
    ATU7.TIORL.BIT.IOD = ATU_InitStruct->ATU_pinDMode; 
}

/**
 * @brief  Allow port to output PWM waveform.
 * @param  ATU_selection: Select ATU for output control.
 *         This parameter can be one of the following values:
 *         ATU34_SEL, ATU67_SEL.
 * @param  allowOutPut：Select Port Status
 * @return None
 */
void ATU_OutPutCon(uint8_t ATU_selection, uint8_t allowOutPut)
{
    if(allowOutPut == ENABLE){
        if(ATU_selection == ATU34_SEL)
        {
            ATU.TOERA.BYTE = 0xff;
        }
        else if(ATU_selection == ATU67_SEL)
        {
            ATU.TOERB.BYTE = 0xff;
        } 
    }
    else
    {
        if(ATU_selection == ATU34_SEL)
        {
            ATU.TOERA.BYTE = 0xc0;
        }
        else if(ATU_selection == ATU67_SEL)
        {
            ATU.TOERB.BYTE = 0xc0;
        }   
    }
    
}

/**
 * @brief  Select the corresponding ATU for initialization based on ATU_SEL.
 * @param  ATU_selection: Select ATU for output control.
 *         This parameter can be one of the following values:
 *         ATU0_ATU7.
 * @param  ATU_InitStruct: pointer to a ATU_InitTypeDefstructure  
 *         that contains the configuration information for the specified  
 *         ATU peripheral.
 * @return None
 */
void ATU_Init(uint8_t ATU_selection, ATU_InitTypeDef* ATU_InitStruct)
{
    if(ATU_selection == ATU0_SEL)
    {
        ATU0_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU1_SEL)
    {
        ATU1_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU2_SEL)
    {
        ATU2_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU3_SEL)
    {
        ATU3_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU4_SEL)
    {
        ATU4_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU6_SEL)
    {
        ATU6_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU7_SEL)
    {
        ATU7_Init(ATU_InitStruct);
    }
    else if(ATU_selection == ATU34_SEL)
    {
        // ATU34_JointUseInit(ATU_InitStruct);
    }
    else if(ATU_selection == ATU67_SEL)
    {
        // ATU67_JointUseInit(ATU_InitStruct);
    }

}

/**
 * @brief  Select the corresponding ATU setting comparison value based on ATU_SEL.
 * @param  ATU_selection: Select ATU for output control.
 *         This parameter can be one of the following values:
 *         ATU0_ATU7.
 * @param  ATU_InitStruct: pointer to a ATU_CompareValueTypedef structure  
 *         that contains the configuration information for the specified  
 *         ATU compare value.
 * @return None
 */
void ATU_CompareValueSet(uint8_t ATU_selection, ATU_CompareValueTypedef* ATU_CompareValue)
{
    if(ATU_selection == ATU0_SEL)
    { 
        ATU0.TGRA = ATU_CompareValue->compareValueA;
        ATU0.TGRB = ATU_CompareValue->compareValueB;
        ATU0.TGRC = ATU_CompareValue->compareValueC;
        ATU0.TGRD = ATU_CompareValue->compareValueD;
        ATU0.TGRE = ATU_CompareValue->compareValueE;
        ATU0.TGRF = ATU_CompareValue->compareValueF;
    }
    else if(ATU_selection == ATU1_SEL)
    {
        ATU1.TGRA = ATU_CompareValue->compareValueA;
        ATU1.TGRB = ATU_CompareValue->compareValueB;
    }
    else if(ATU_selection == ATU2_SEL)
    {
        ATU2.TGRA = ATU_CompareValue->compareValueA;
        ATU2.TGRB = ATU_CompareValue->compareValueB;
    }
    else if(ATU_selection == ATU3_SEL)
    {
        ATU3.TGRA = ATU_CompareValue->compareValueA;
        ATU3.TGRB = ATU_CompareValue->compareValueB;
        ATU3.TGRC = ATU_CompareValue->compareValueC;
        ATU3.TGRD = ATU_CompareValue->compareValueD;
        ATU3.TGRE = ATU_CompareValue->compareValueE;
    }
    else if(ATU_selection == ATU4_SEL)
    {
        ATU4.TGRA = ATU_CompareValue->compareValueA;
        ATU4.TGRB = ATU_CompareValue->compareValueB;
        ATU4.TGRC = ATU_CompareValue->compareValueC;
        ATU4.TGRD = ATU_CompareValue->compareValueD;
        ATU4.TGRE = ATU_CompareValue->compareValueE;
        ATU4.TGRF = ATU_CompareValue->compareValueF;
    }
    else if(ATU_selection == ATU6_SEL)
    {
        ATU6.TGRA = ATU_CompareValue->compareValueA;
        ATU6.TGRB = ATU_CompareValue->compareValueB;
        ATU6.TGRC = ATU_CompareValue->compareValueC;
        ATU6.TGRD = ATU_CompareValue->compareValueD;
        ATU6.TGRE = ATU_CompareValue->compareValueE;
    }
    else if(ATU_selection == ATU7_SEL)
    {
        ATU7.TGRA = ATU_CompareValue->compareValueA;
        ATU7.TGRB = ATU_CompareValue->compareValueB;
        ATU7.TGRC = ATU_CompareValue->compareValueC;
        ATU7.TGRD = ATU_CompareValue->compareValueD;
        ATU7.TGRE = ATU_CompareValue->compareValueE;
        ATU7.TGRF = ATU_CompareValue->compareValueF;
    }
}

/**
 * @brief  Enable ATU for counting.
 * @param  startGroup: Enable ATU combination.
 *         This parameter can be one of the following values:
 *         Combination of ATU0-SEL-ATU7-SEL.
 * @return None
 */
void ATU_StartCnt(uint8_t startGroup)
{
    ATU.TCSYSTR.BYTE = startGroup;
}
